import Vue from 'vue'
import {
	getUserInfo,
	login,
	loginSms,
	loginMail,
	logout
} from '@/api/user'
import {
	getToken,
	removeToken,
	setToken
} from '@/utils/token'
import {
	tokenName
} from '@/config'

const state = () => ({
	token: getToken(),
	username: '',
	avatar: '',
	userInfo: {},
})

const getters = {
	token: (state) => state.token,
	username: (state) => state.username,
	avatar: (state) => state.avatar,
	userInfo: (state) => state.userInfo,
}

const mutations = {
	/**
	 * @description 设置token
	 * @param {*} state
	 * @param {*} token
	 */
	setToken(state, token) {
		state.token = token
		setToken(token)
	},
	/**
	 * @description 设置用户名
	 * @param {*} state
	 * @param {*} username
	 */
	setUsername(state, username) {
		state.username = username
	},
	/**
	 * @description 设置头像
	 * @param {*} state
	 * @param {*} avatar
	 */
	setAvatar(state, avatar) {
		state.avatar = avatar
	},
	setUserInfo(state, userInfo) {
		state.userInfo = userInfo
	},
}

const actions = {
	/**
	 * @description 登录
	 * @param {*} { commit }
	 * @param {*} userInfo
	 */
	async login({
		commit
	}, userInfo) {
		console.log(userInfo)
		const {
			data
		} = await login(userInfo)
		if (data) {
			commit('setToken', data[tokenName])
			commit('setAvatar', data?.avatar)
			commit('setUsername', data?.nickname)
			commit('setUserInfo', data)
		} else {
			const err = `登录接口异常，未正确返回${tokenName}...`
			throw err
		}
	},

	async loginSms({
		commit
	}, userInfo) {
		const {
			data
		} = await loginSms(userInfo)
		if (data) {
			commit('setToken', data[tokenName])
			commit('setAvatar', data?.avatar)
			commit('setUsername', data?.nickname)
			commit('setUserInfo', data)
		} else {
			const err = `登录接口异常，未正确返回${tokenName}...`
			throw err
		}
	},

	async loginMail({
		commit
	}, userInfo) {
		const {
			data
		} = await loginMail(userInfo)
		if (data) {
			commit('setToken', data[tokenName])
			commit('setAvatar', data?.avatar)
			commit('setUsername', data?.nickname)
			commit('setUserInfo', data)
		} else {
			const err = `登录接口异常，未正确返回${tokenName}...`
			throw err
		}
	},

	/**
	 * @description 获取用户信息接口 这个接口非常非常重要，如果没有明确底层前逻辑禁止修改此方法，错误的修改可能造成整个框架无法正常使用
	 * @param {*} { commit, dispatch, state }
	 * @returns
	 */
	async getUserInfo({
		commit,
		dispatch
	}) {
		const {
			data
		} = await getUserInfo()
		if (data) {
			commit('setToken', data[tokenName])
			commit('setAvatar', data?.avatar)
			commit('setUsername', data?.nickname)
			commit('setUserInfo', data)
			console.log(data)
		} else {
			const err = `登录接口异常，未正确返回${tokenName}...`
			throw err
		}
	},

	/**
	 * @description 退出登录
	 * @param {*} { dispatch }
	 */
	async logout({
		dispatch
	}) {
		await logout()
		await dispatch('resetAll')
	},
	/**
	 * @description 重置token、用户信息等
	 * @param {*} { commit, dispatch }
	 */
	async resetAll({
		commit,
		dispatch
	}) {
		await dispatch('setToken', '')
		await dispatch('setAvatar', '')
		await dispatch('setUsername', '')
		await dispatch('setUserInfo', {})
		removeToken()
	}
}

export default {
	state,
	getters,
	mutations,
	actions
}